package com.qire.manhua.model.viewModel.impl;

import android.view.View;
import android.widget.BaseAdapter;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.FriendPaidController;
import com.qire.manhua.dal.service.FriendPaidService;
import com.qire.manhua.databinding.LayoutFriendPaidItemBinding;
import com.qire.manhua.model.entity.ShareInfo;

import java.util.ArrayList;

public class FriendPaidViewModel extends BaseViewModel implements IViewModel {

    private FriendPaidService paidService = new FriendPaidService(this);

    private final ArrayList<FriendPaidItemViewModel> coinsPaidItemList = new ArrayList<>();
    private final ArrayList<FriendPaidItemViewModel> membershipPaidItemList = new ArrayList();

    private final SummerBindAdapter coinsPaidAdapter = new SummerBindAdapter(coinsPaidItemList);
    private final SummerBindAdapter membershipPaidAdapter = new SummerBindAdapter(membershipPaidItemList);

    private FriendPaidItemViewModel selectPaidItem;

    private ShareInfo shareInfo;

    private boolean isPaidVisible = false;

    public FriendPaidViewModel() {
        refreshWaresUI();
    }

    @Override
    public int getVariableId() {
        return BR.friendPaidViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition(FriendPaidController controller) {
        coinsPaidAdapter.setEventProcess(controller);
        membershipPaidAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        this.paidService = null;
        coinsPaidAdapter.setEventProcess(null);
        membershipPaidAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    //region    #业务数据 begin

    public void loadData() {
        paidService.loadPaidData();
    }

    public void clearCoinsPaidItemList() {
        coinsPaidItemList.clear();
    }
    public void addCoinsPaidItem(int payFee, boolean isFirstPay, String waresFee, String waresDescribe, String waresRewardFee, boolean haveReward) {
        coinsPaidItemList.add(new FriendPaidItemViewModel(1, payFee, isFirstPay, waresFee, waresDescribe, waresRewardFee, haveReward));
    }

    public void clearMembershipPaidItemList() {
        membershipPaidItemList.clear();
    }
    public void addMembershipPaidItem(int payFee, boolean isFirstPay, String waresFee, String waresDescribe, String waresRewardFee) {
        membershipPaidItemList.add(new FriendPaidItemViewModel(2, payFee, isFirstPay, waresFee, waresDescribe, waresRewardFee, false));
    }

    public void goFriendPaidPanel() {
        if (ObjectUtil.nonNull(selectPaidItem)) {
            paidService.createFriendPaidOrder(selectPaidItem.payType, selectPaidItem.payFee);
        }
    }

    public void setShareInfo(ShareInfo shareInfo) {
        this.shareInfo = shareInfo;
    }

    public ShareInfo shareInfo() {
        return shareInfo;
    }

    public boolean isPaidVisible() {
        return isPaidVisible;
    }

    //endregion #业务数据 end

    //region    #UI操作 begin

    public void refreshUI() {
        coinsPaidAdapter.notifyDataSetChanged();
        membershipPaidAdapter.notifyDataSetChanged();
    }

    public void refreshWaresUI() {
        isPaidVisible = false;
        set("waresPanelVisible", View.VISIBLE);
        set("paidPanelVisible", View.GONE);
    }

    public void refreshPaidUI(String paidFee, String qrCodeUrl) {
        isPaidVisible = true;
        set("waresPanelVisible", View.GONE);
        set("paidPanelVisible", View.VISIBLE);
        set("paidFee", paidFee);
        set("paidQrCode", qrCodeUrl);
    }

    //endregion #UI操作 end

    //region    #UI绑定 begin

    public MutableLiveData<Integer> getWaresPanelVisible() {
        return get("waresPanelVisible");
    }
    public MutableLiveData<Integer> getPaidPanelVisible() {
        return get("paidPanelVisible");
    }

    public MutableLiveData<CharSequence> getPaidFee() {
        return get("paidFee");
    }
    public MutableLiveData<String> getPaidQrCode() {
        return get("paidQrCode");
    }

    public BaseAdapter getCoinsPaidAdapter() {
        return coinsPaidAdapter;
    }

    public BaseAdapter getMembershipPaidAdapter() {
        return membershipPaidAdapter;
    }
    //endregion #UI绑定 end

    @AdapterEntityBind(viewRid = R.layout.layout_friend_paid_item)
    public class FriendPaidItemViewModel extends BaseObservable implements AutoVariable<LayoutFriendPaidItemBinding> {

        public final int payType;
        public final int payFee;

        private boolean isFirstPay;
        private String waresFee;
        private String waresDescribe;
        private String waresRewardFee;
        private boolean haveReward;

        public FriendPaidItemViewModel(int payType, int payFee, boolean isFirstPay, String waresFee, String waresDescribe, String waresRewardFee, boolean haveReward) {
            this.payType = payType;
            this.payFee = payFee;
            this.isFirstPay = isFirstPay;
            this.waresFee = waresFee;
            this.waresDescribe = waresDescribe;
            this.waresRewardFee = waresRewardFee;
            this.haveReward = haveReward;
        }

        @Override
        public void bindVariable(LayoutFriendPaidItemBinding viewBind) {
            viewBind.setFriendPaidItemViewModel(this);
        }

        public void toggle() {
            if(ObjectUtil.nonNull(selectPaidItem) && !equals(selectPaidItem)) {
                selectPaidItem.toggle();
            }
            selectPaidItem = isSelect() ? null : this;
            notifyChange();
        }

        public boolean isSelect() {
            return equals(selectPaidItem);
        }

        @Bindable
        public boolean getChecked() {
            return isSelect();
        }

        @Bindable
        public CharSequence getWaresFee() {
            return waresFee;
        }
        @Bindable
        public String getWaresFeeTextColor() {
            return isSelect() ? "#ff9204" : "#333333";
        }
        @Bindable
        public CharSequence getWaresDescribe() {
            return waresDescribe;
        }
        @Bindable
        public CharSequence getWaresRewardFee() {
            return waresRewardFee;
        }
        @Bindable
        public String getWaresRewardFeeTextColor() {
            return isSelect() ? "#ff9204" : "#909090";
        }
        @Bindable
        public int getCornerMarkVisible() {
            return haveReward ? View.VISIBLE : View.GONE;
        }
        @Bindable
        public int getCornerMarkRid() {
            return isFirstPay ? R.drawable.app_friend_paid_icon_first : R.drawable.app_friend_paid_icon_reward;
        }
    }

} 
